package io.github.talelin.latticy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.github.talelin.autoconfigure.exception.NotFoundException;
import io.github.talelin.latticy.dto.article.CreateOrUpdateArticleDTO;
import io.github.talelin.latticy.dto.article.ExpressDTO;
import io.github.talelin.latticy.mapper.*;
import io.github.talelin.latticy.model.*;
import io.github.talelin.latticy.service.ArticleService;
import io.github.talelin.latticy.vo.ArticleVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author generator@lms
 * @since 2021-04-21
 */
@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, ArticleDO> implements ArticleService {
    @Autowired
    private CommentMapper commentMapper;

    @Autowired
    private CommentReplyMapper commentReplyMapper;

    @Autowired
    private ArticleGreatMapper articleGreatMapper;

    @Autowired
    private ArticleCollectMapper articleCollectMapper;

    @Override
    public ArticleVO getArticle(Integer id) {
        ArticleDO article = this.getById(id);
        //文章判空
        if (article == null) {
            throw new NotFoundException(90000);
        }
        //获取该文章下的一级评论
        List<CommentDO> comments = commentMapper.selectList(new QueryWrapper<CommentDO>()
                .eq("article_id", id)
                .eq("type",1)
                .orderByAsc("create_time"));
        comments.forEach(c -> {
            //获取一级评论下所有的回复
            List<CommentReplyDO> commentReplyList = commentReplyMapper.selectList(new QueryWrapper<CommentReplyDO>()
                    .eq("comment_id", c.getId())
                    .orderByAsc("create_time"));
            c.setReplyList(commentReplyList);
        });
        return ArticleVO.builder()
                .article(article)
                .comments(comments)
                .commentCount(comments.size())
                .build();
    }

    @Override
    @Transactional
    public Map<String, Integer> addLike(Integer id, Integer uid) {

        QueryWrapper<ArticleGreatDO> wrapper = new QueryWrapper<>();
        wrapper.eq("u0id", id).eq("u1id", uid);
        ArticleGreatDO articleGreat = articleGreatMapper.selectOne(wrapper);
        Map<String, Integer> map = new HashMap<>();
        if (articleGreat != null) {
            //如果找到了这条记录，则删除该记录，同时点赞数减1
            articleGreatMapper.delete(wrapper);
            //点赞数减1
            ArticleDO article = this.getBaseMapper().selectById(id);
            map.put("Likes", article.getLikes() - 1);
            map.put("Status", 0);
            article.setLikes(article.getLikes() - 1);
            this.getBaseMapper().updateById(article);
        }else{
            //如果没有找到这条记录，则添加这条记录，同时点赞数加1
            ArticleGreatDO great = new ArticleGreatDO();
            great.setU0id(id);
            great.setU1id(uid);
            //添加记录
            articleGreatMapper.insert(great);
            //点赞数加一
            ArticleDO article = this.getBaseMapper().selectById(id);
            map.put("Likes", article.getLikes() + 1);
            map.put("Status", 1);
            article.setLikes(article.getLikes() + 1);
            this.getBaseMapper().updateById(article);
        }
        return map;
    }

    @Override
    public Map<String, Integer> addCollect(Integer id, Integer uid) {

        QueryWrapper<ArticleCollectDO> wrapper = new QueryWrapper<>();
        wrapper.eq("u0id", id).eq("u1id", uid);
        ArticleCollectDO articleGreat = articleCollectMapper.selectOne(wrapper);
        Map<String, Integer> map = new HashMap<>();
        if (articleGreat != null) {
            //如果找到了这条记录，则删除该记录，同时收藏数减1
            articleCollectMapper.delete(wrapper);
            //收藏数减1
            ArticleDO article = this.getBaseMapper().selectById(id);
            map.put("Collects", article.getCollects() - 1);
            map.put("Status", 0);
            article.setLikes(article.getCollects() - 1);
            this.getBaseMapper().updateById(article);
        }else{
            //如果没有找到这条记录，则添加这条记录，同时收藏数加1
            ArticleCollectDO great = new ArticleCollectDO();
            great.setU0id(id);
            great.setU1id(uid);
            //添加记录
            articleCollectMapper.insert(great);
            //收藏数加一
            ArticleDO article = this.getBaseMapper().selectById(id);
            map.put("Collects", article.getCollects() + 1);
            map.put("Status", 1);
            article.setLikes(article.getCollects() + 1);
            this.getBaseMapper().updateById(article);
        }
        return map;
    }

    @Override
    public void update(CreateOrUpdateArticleDTO dto, Integer id) {
        ArticleDO article = this.getById(id);
        if (article == null) {
            throw new NotFoundException(90000);
        }
        BeanUtils.copyProperties(dto,article);
        this.updateById(article);
    }

    @Override
    public void create(CreateOrUpdateArticleDTO dto) {
        ArticleDO article = new ArticleDO();
        BeanUtils.copyProperties(dto,article);
        this.save(article);
    }

    @Override
    public void checkArticle(Integer audit, Integer id) {
        ArticleDO article = this.getById(id);
        if (article == null) {
            throw new NotFoundException(90000);
        }
        article.setAudit(audit);
        this.updateById(article);
    }

    @Override
    public void issueExpress(ExpressDTO dto) {
        ArticleDO article = new ArticleDO();
        BeanUtils.copyProperties(dto,article);
        this.save(article);
    }
}
